import axios from 'axios';
import router from '../router/index.js'
import { Message } from 'element-ui'
// import env from '/public/config/dev.env'
import env from '/public/config/prod.env'

// 后端服务器根路径
let BaseURL = env.BASE_URL;
export {BaseURL}

const getRequest = (url) => {
    return axios({
        method: 'get',
        url: `${BaseURL}${url}`,
    })
}

const postRequest = (url, param) => {
    return axios({
        method: 'post',
        url: `${BaseURL}${url}`,
        data: param,
    })
}

const deleteRequest = (url) => {
    return axios({
        method: 'delete',
        url: `${BaseURL}${url}`,
    })
}

const putRequest = (url, param) => {
    return axios({
        method: 'put',
        url: `${BaseURL}${url}`,
        data: param,
    })
}
const http = (url, method, param) => {
    if (method == 'get' || method == 'GET') {
        return getRequest(url);
    }

    if (method == 'post' || method == 'POST') {
        return postRequest(url, param);
    }

    if (method == 'put' || method == 'PUT') {
        return putRequest(url, param);
    }

    if (method == 'delete' || method == 'DELETE') {
        return deleteRequest(url);
    }
}


const AUTH_TOKEN = sessionStorage.getItem('token')
export  {AUTH_TOKEN}


// 请求拦截器
axios.interceptors.request.use(function (config) {
    if (!config.url.includes('/login')) {
        config.headers.Authorization = 'Bearer ' + sessionStorage.getItem('token')
    }
    return config
}, function (error) {
    return Promise.reject(error)
})

// 响应拦截器
axios.interceptors.response.use((success) => {
    if (success.status && success.status === 200) {
        // 业务逻辑错误
        let code = success.data.code;
        if (code === 500 || code === 404 || code === 403) {
            Message.warning(success.data.message)
            return
        }
        if (success.data.code === 401) {
            Message.warning(success.data.message)
            router.replace("/")
            return
        }

        // 业务逻辑正确
        if (code === 200) {
            // 刷新token
            // sessionStorage.setItem('token',success.config.headers['refresh_token'])
            if (success.data.message) {
                Message.success(success.data.message)
            }
        }
    }
    return success.data.data
}, error => {
    if (!error.response || !error.response.status) {
        console.log(error)
        return;
    }
    if (error.response.status === 504 || error.response.status === 404) {
        Message.info('无效的请求地址')
    } else if (error.response.status === 403) {
        Message.info('权限不足')
    } else if (error.response.status === 401) {
        Message.info('请登录')
        Message.replace('/')
    } else {
        if (error.response.data.message) {
            Message.error(error.response.data.message)
        } else {
            Message.error('未知错误')
        }
    }
    return
})


export {http}


// 定义vue 插件 【插件 (Plugins) 是一种能为 Vue 添加全局功能的工具代码。】
export const net = {
    install(Vue, options) {
        // 注入一个全局可用的 $translate() 方法
        Vue.prototype.$http = http;
    }
}